$(function (){
	/*$("body").on("mouseover",function(){
		var controlNo=$(".itemEditForm").find(".form-group .form-control").attr("class");
		if(controlNo=="left form-control form-controlNo basicinfoinput"){
			$(".itemEditForm").find(".form-group .form-control").attr("readonly",true);
			$(".star").hide();
		}else{
			$(".star").show();
			$(".itemEditForm").find(".form-group .form-control").attr("readonly",false);
			$("#donkeyNum").attr("disabled",true);
		};
		if($("#companyName")[0].value && $("#memberName")[0].value && $("#companyAddress")[0].value && $("#websiteUrl")[0].value && $("#realName")[0].value && $("#bankName")[0].value
			&& $("#openBank")[0].value && emailpattern() && (telephone()||!$("#FixedPhone").val()) && mobilepattern() && (mailboxpattern() || !$("#adminemail").val()) && accountNum()){
			$("#comfirmUpdate").attr("disabled",false);
		}else{
			$("#comfirmUpdate").attr("disabled",true);
		}
	});*/
	$("body").on("mouseover",function(){
		if($("#companyName")[0].value && $("#memberName")[0].value && $("#companyAddress")[0].value && $("#websiteUrl")[0].value && $("#realName")[0].value && $("#bankName")[0].value
			&& $("#openBank")[0].value && emailpattern() && (telephone()||!$("#FixedPhone").val()) && mobilepattern() && (mailboxpattern() || !$("#adminemail").val()) && accountNum()){
			$("#comfirmUpdate").attr("disabled",false);
		}else{
			$("#comfirmUpdate").attr("disabled",true);
		}
	});
	$(".star").hide();
	$(".form-controlNo").on("mouseover",function(){
		$(this).attr("title",$(this).context.value).css("cursor","pointer");
	});
    
	unit(); 
	$("#returnBtn").hide();
	$("#returnBtn").on("click",function(){
		unit();
	});
	function unit(){
		//基本信息
		var loginReturnBean;
		$.ajax({
			url : '/web-adv/basicInfo',
			data : loginReturnBean,
			type : 'post',
			dataType : 'json',
			success : function(data){
				$('#companyName').val(data.companyName);
				$('#companyName').siblings(".basicinfoinputShow").text(data.companyName);
				$('#donkeyNum').val(data.donkeyNum);
				$('#donkeyNum').siblings(".basicinfoinputShow").text(data.donkeyNum);
				$('#memberName').val(data.memberName);
				$('#memberName').siblings(".basicinfoinputShow").text(data.memberName);
				$('#creditRat').val(data.creditRating);
				$('#creditRat').siblings(".basicinfoinputShow").text(data.creditRating);
				$('#companyAddress').val(data.companyAddress);
				$('#companyAddress').siblings(".basicinfoinputShow").text(data.companyAddress);
				$('#email').val(data.email);
				$('#email').siblings(".basicinfoinputShow").text(data.email);
				$('#oldEmail').val(data.email);
				$('#oldEmail').siblings(".basicinfoinputShow").text(data.email);
				$('#websiteUrl').val(data.websiteUrl);
				$('#websiteUrl').siblings(".basicinfoinputShow").text(data.websiteUrl);
				$('#realName').val(data.realName);
				$('#realName').siblings(".basicinfoinputShow").text(data.realName);
				$('#FixedPhone').val(data.fixedPhone);
				$('#FixedPhone').siblings(".basicinfoinputShow").text(data.fixedPhone);
				$('#mobilePhone').val(data.mobilePhone);
				$('#mobilePhone').siblings(".basicinfoinputShow").text(data.mobilePhone);
				$('#adminemail').val(data.adminMail);
				$('#adminemail').siblings(".basicinfoinputShow").text(data.adminMail);
				$('#bankName').val(data.bankName);
				$('#bankName').siblings(".basicinfoinputShow").text(data.bankName);
				$('#openBank').val(data.openBank);
				$('#openBank').siblings(".basicinfoinputShow").text(data.openBank);
				$('#accountNumbe').val(data.accountNumber);
				$('#accountNumbe').siblings(".basicinfoinputShow").text(data.accountNumber);
				$('#qq').val(data.qq);
				$('#qq').siblings(".basicinfoinputShow").text(data.qq);
				$('#flag').val(data.advId);
				//图片显示
				$('#logo_img').attr("src", data.companyLogoUrl);
				$('#buss_img').attr("src", data.businessLicenceUrl);
				$('#org_img').attr("src", data.organizationCodeUrl);
				$('#flag').val(data.advId);

				//图片地址（隐藏域）
				$("#logo_url").val( data.companyLogo);
				$("#buss_url").val( data.businessLicence);
				$("#org_url").val( data.organizationCode);
				initUpload();
			},
			error : function(){
			}
		})
	};
	
	//正则验证
	function emailpattern(){  //E-mail
		var emailVal=$("#email").val();
		var patEmail=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(patEmail.test(emailVal)){
			return true;
		}else{
			return false;
		}
	};
	function telephone(){ //固定电话
		var phoneVal=$("#FixedPhone").val();
		var phone=/^0\d{2,3}-?\d{7,8}$/;
		if(phone.test(phoneVal)){
			return true;
		}else{
			return false;
		}
	};
	function accountNum(){  //开户账号
		var accountVal=$("#accountNumbe").val();
		var account=/^(\d{16}|\d{19})$/;
		if(account.test(accountVal)){
			return true;
		}else{
			return false;
		}
	};
	function mobilepattern(){  //手机号
		var mobileVal=$("#mobilePhone").val();
		var mobilePhone=/^[1][3578][0-9]{9}$/;
		if(mobilePhone.test(mobileVal)){
			return true;
		}else{
			return false;
		}
	};
	function mailboxpattern(){  //邮箱
		var mailboxVal=$("#adminemail").val();
		var mailbox=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(mailbox.test(mailboxVal)){
			return true;
		}else{
			return false;
		}
	};
	//昵称
	function usernamepat(){
		var usernameVal=$("#memberName").val();
		var patUsername=/^[a-zA-Z0-9_\u4e00-\u9fa5]{5,25}$/;
		if(patUsername.test(usernameVal)){
			return true;
		}else{
			return false;
		}
	};
	$("#userNamePat").hide();
	$("#memberName").on("blur",function(){
		var result=usernamepat();
		if(!$("#memberName").val()){
			$("#userNamePat").html("*必填").css("right","0").show();
			$("#repeatAlert").hide();
		}else if(result){
			$("#userNamePat").hide();
		}else{
			$("#userNamePat").html("*可由中英文、数字、下划线组成5-25个字符").css("right","-238px").show();
		}
	});
	//正则验证
	$("#phonePat").hide();
	$("#FixedPhone").on("blur",function(){
		var result=telephone();
		if(result){
			$("#phonePat").hide();
		}else if(!$(this).val()){
			$("#phonePat").hide();
		}else{
			$("#phonePat").show();
		}
	});
	$("#accountPat").hide();
	$("#accountNumbe").on("blur",function(){
		var result=accountNum();
		if(result){
			$("#accountPat").hide();
		}else if(!$(this).val()){
			$("#accountPat").hide();
		}else{
			$("#accountPat").show();
		}
	});
	//E-mail
	$("#emailPat").hide();
	$("#email").on("blur",function(){
		var result=emailpattern();
		if(result){
			$("#emailPat").hide();
		}else{
			$("#emailPat").show();
		}
	});
	//手机号
	$("#mobilePat").hide();
	$("#mobilePhone").on("blur",function(){
		var result=mobilepattern();
		if(result){
			$("#mobilePat").hide();
		}else{
			$("#mobilePat").show();
		}
	});
	//邮箱
	$("#mailboxPat").hide();
	$("#adminemail").on("blur",function(){
		var result=mailboxpattern();
		if(result){
			$("#mailboxPat").hide();
		}else{
			$("#mailboxPat").show();
		}
	});
})

function initUpload() {
	new AjaxUpload("upload_logo", {
		action: "/web-adv/uploadChaImg",
		name: "card",
		data: {},
		onSubmit: function (file, extension) {
			extension = extension.toUpperCase();
			if (extension != "BMP" && extension != "PNG" && extension != "GIF" && extension != "JPEG" && extension != "JPG") {
				alert("只支持图片文件！");
				return false;
			}
		},
		onComplete : function(file,data){
			var dataObj=eval("("+data+")");
			if(dataObj.status==1){
				$("#logo_url").val(dataObj.data);
				$("#logo_img").attr("src",dataObj.picServerPrex+dataObj.data);
			}
			else{
				alert("上次失败！");
			}
		}
	});

	new AjaxUpload("upload_business_license", {
		action: "/web-adv/uploadChaImg",
		name: "card",
		data: {},
		onSubmit: function (file, extension) {
			extension = extension.toUpperCase();
			if (extension != "BMP" && extension != "PNG" && extension != "GIF" && extension != "JPEG" && extension != "JPG") {
				alert("只支持图片文件！");
				return false;
			}
		},
		onComplete: function (file, result) {
			var dataObj=eval("("+result+")");
			if (dataObj.status == 1) {
				$("#buss_url").val(dataObj.data);
				$("#buss_img").attr("src",dataObj.picServerPrex+dataObj.data);
			} else {
				alert("上次失败！");
			}
		}
	});

	new AjaxUpload("upload_organization", {
		action: "/web-adv/uploadChaImg",
		name: "card",
		data: {},
		onSubmit: function (file, extension) {
			extension = extension.toUpperCase();
			if (extension != "BMP" && extension != "PNG" && extension != "GIF" && extension != "JPEG" && extension != "JPG") {
				alert("只支持图片文件！");
				return false;
			}
		},
		onComplete: function (file, result) {
			var dataObj=eval("("+result+")");
			if (dataObj.status == 1) {
				$("#org_url").val(dataObj.data);
				$("#org_img").attr("src",dataObj.picServerPrex+dataObj.data);
			}
			else {
				alert("上次失败！");
			}
		}
	});
}